package com.brillilab.starter.task;

import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.common.redis.RedisCache;
import com.brillilab.domain.enums.method.MethodDealStateEnum;
import com.brillilab.domain.enums.method.MethodSourceEnum;
import com.brillilab.domain.enums.method.RecommendMethodSyncStatesEnum;
import com.brillilab.domain.po.method.Method;
import com.brillilab.domain.po.method.MethodRecommendReceive;
import com.brillilab.domain.po.method.RecommendMethodSync;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.method.IMethodRecommendService;
import com.brillilab.service.core.method.IMethodService;
import com.brillilab.service.core.method.IRecommendMethodSyncService;
import com.brillilab.service.logic.method.MethodRecommendLogic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Wu Menghao
 * @description 全局定时任务 需要使用新增新的方法
 * @time 2019/06/11
 */
@Component
@Slf4j
public class RecommendMethodSyncTask {

    @Resource
    private IRecommendMethodSyncService recommendMethodSyncService;
    @Resource
    private IMethodService methodService;
    @Resource
    private MethodRecommendLogic methodRecommendLogic;
    @Resource
    private RedisCache redisCache;

    /**
     * 注册用户推荐方法同步任务
     */
    @Scheduled(cron="0/2 * * * * ?")
    private void recommendMethodSync() {
        final String lockKey="METHOD_SYNC_ID_LOCK";
        if(redisCache.getObj(lockKey) == null){
            RecommendMethodSync sync=recommendMethodSyncService.selectTopOne();
            if(sync!=null){
                try {
                    //用户信息
                    UserInfoVo user=new UserInfoVo();
                    user.setId(sync.getUserId());
                    user.setLabMemberId(sync.getLabMemberId());
                    user.setLabId(sync.getLabId());
                    user.setLabName(sync.getLabName());
                    //来源方法
                    Method oriMethod=methodService.getById(sync.getOriMethodId());
                    MethodSourceEnum source=MethodSourceEnum.RECOMMEND;
                    //推荐接受
                    MethodRecommendReceive receive=new MethodRecommendReceive();
                    receive.setLabId(user.getLabId());
                    receive.setLabMemberId(user.getLabMemberId());
                    receive.setMethodId(sync.getOriMethodId());
                    receive.setState(MethodDealStateEnum.Accept.getState());
                    receive.setMethodRecommendId(sync.getRecommendId());
                    //同步状态更新
                    sync.setStates(RecommendMethodSyncStatesEnum.SUCCESS.getValue());

                    methodRecommendLogic.executeMethodSync(sync,user,oriMethod,source,receive);

                } catch (Exception e) {
                    if(e instanceof BrillilabException){
                        return;
                    }
                    log.error("推荐方法同步失败！",e);
                    sync.setStates(RecommendMethodSyncStatesEnum.FAILURE.getValue());
                    recommendMethodSyncService.update(sync);
                    redisCache.delete(lockKey);
                }
            }

        }
    }

}
